package qq.android.wan.qq.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import qq.android.wan.qq.db.MyDateBaseHelper;
import qq.android.wan.qq.entity.FriendBean;
import qq.android.wan.qq.utils.Constant;

/**
 * Created by wxx on 2016/4/5.
 */
public class FriendDao {
    private SQLiteDatabase db;
    private MyDateBaseHelper helper;
    public FriendDao() {

    }
    public FriendDao(Context context) {
        helper = new MyDateBaseHelper(context);
    }

    public static void createTable(SQLiteDatabase db) {
        db.execSQL("create table " + Constant.DB_FRIEND_NAME + "(" +
                "userId integet" +
                ",nick text"+")");
    }

    public void insert(FriendBean friendBean) {
        db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", friendBean.getUserId());
        values.put("nick", friendBean.getNick());
        db.insert(Constant.DB_FRIEND_NAME, null, values);
        db.close();
    }
    public void insert(SQLiteDatabase sqLiteDatabase,FriendBean friendBean) {
        ContentValues values = new ContentValues();
        values.put("userId", friendBean.getUserId());
        values.put("nick", friendBean.getNick());
        sqLiteDatabase.insert(Constant.DB_FRIEND_NAME, null, values);
        sqLiteDatabase.close();
    }

    public List<FriendBean> getAllFriends() {
        db = helper.getReadableDatabase();
        List<FriendBean> lists = new ArrayList<FriendBean>();
        Cursor cursor = db.query(Constant.DB_FRIEND_NAME, null, null, null, null, null, null);
        if (cursor.getCount() != 0) {
            while (cursor.moveToNext()) {
                FriendBean bean = new FriendBean();
                bean.setUserId(cursor.getInt(cursor.getColumnIndex("userId")));
                bean.setNick(cursor.getString(cursor.getColumnIndex("nick")));
                lists.add(bean);
            }
        }
        cursor.close();
        db.close();
        return lists;
    }

    public void insertList(List<FriendBean> friends) {
        db = helper.getWritableDatabase();
        for (FriendBean bean : friends) {
            ContentValues values = new ContentValues();
            values.put("userId", bean.getUserId());
            values.put("nick", bean.getNick());
            db.insert(Constant.DB_FRIEND_NAME, null, values);
        }
        db.close();
    }

    public FriendBean queryFriendByid(String userId) {
        db = helper.getReadableDatabase();
        FriendBean bean = new FriendBean();
        Cursor cursor = db.query(Constant.DB_FRIEND_NAME, null, "userId=?", new String[]{userId}, null, null, null);
        if (cursor.getCount() != 0) {
            bean.setUserId(cursor.getInt(cursor.getColumnIndex("userId")));
            bean.setNick(cursor.getString(cursor.getColumnIndex("nick")));
        }
        db.close();
        return bean;
    }
}
